package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.entity.Blog;
import com.markerhub.entity.Type;
import com.markerhub.entity.User;
import com.markerhub.mapper.BlogMapper;
import com.markerhub.mapper.TypeMapper;
import com.markerhub.mapper.UserMapper;
import com.markerhub.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.markerhub.views.Taglist;
import com.markerhub.views.Typelist;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-04-10
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {
    @Autowired
    BlogMapper blogMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    TypeMapper typeMapper;
    //获得博客分类信息
    @Override
   public List<Typelist> getTpyelist(){
       return blogMapper.getTypelist();
   }

    //获得博客标签信息
    @Override
    public List<Taglist> getTaglist(){
        return blogMapper.getTaglist();
    }

    //分页查询博客
    @Override
    public IPage<Blog> selectBlogs(long current, long size, String column, int sort,String querycontent){
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        if(querycontent!=null)
            queryWrapper.like("title",querycontent).or().like("content",querycontent);
        //设置升序或者降序
        if( 0 == sort){
            //升序
            queryWrapper.orderByAsc(column);
        }else{
            //降序
            queryWrapper.orderByDesc(column);
        }
        return blogMapper.selectPage(new Page<Blog>(current,size),queryWrapper);
    }
    //根据id查询blog
    @Override
    public Blog selectBlogbyId(Integer id)
    {
        return blogMapper.selectById(id);
    }
    //根据type(类型)查询blog
    @Override
    public IPage<Blog> selectBlogsbyType(long current, long size, String column, int sort,Integer type_id){
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id",type_id);
        //设置升序或者降序
        if( 0 == sort){
            //升序
            queryWrapper.orderByAsc(column);
        }else{
            //降序
            queryWrapper.orderByDesc(column);
        }
        return blogMapper.selectPage(new Page<Blog>(current,size),queryWrapper);
    }
    //根据tag(标签)查询blog
    @Override
    public IPage<Blog> selectBlogsbyTag(long current, long size, String column, int sort,Integer tag_id){
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_id",tag_id);
        //设置升序或者降序
        if( 0 == sort){
            //升序
            queryWrapper.orderByAsc(column);
        }else{
            //降序
            queryWrapper.orderByDesc(column);
        }
        return blogMapper.selectPage(new Page<Blog>(current,size),queryWrapper);
    }

    @Override
    public void addBlog(String title, Integer uid,String content,String firstPicture,String flag,Boolean recommend,Boolean appreciation,Boolean commentabled,Integer tagId,Integer typeId,String shareStatement,String description)
    {
        Blog blog=new Blog();
        Date data=new Date();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(data.toInstant(), zoneId);
        blog.setCreateTime(localDateTime);
        blog.setTitle(title);
        blog.setUid(uid);
        blog.setContent(content);
        blog.setFirstPicture(firstPicture);
        blog.setFlag(flag);
        blog.setRecommend(recommend);
        blog.setAppreciation(appreciation);
        blog.setCommentabled(commentabled);
        blog.setTagId(tagId);
        blog.setTypeId(typeId);
        blog.setShareStatement(shareStatement);
        blog.setDescription(description);
        User user=userMapper.selectById(uid);
        blog.setNickname(user.getNickname());
        blog.setAvatar(user.getAvatar());
        String typename=typeMapper.selectById(typeId).getTypeName();
        blog.setTypeName(typename);
        blogMapper.insert(blog);
    }

    @Override
    public void changeblog(Blog blog)
    {
        blogMapper.updateById(blog);
    }

    @Override
    public void deleteBlog(Integer blogid)
    {
        blogMapper.deleteById(blogid);
    }
}
